package com.artisan.web.action;

import com.artisan.common.pojo.PageBean;
import com.artisan.common.pojo.PageParam;
import com.artisan.commonweb.result.ActionResult;
import com.artisan.medium.admin.service.BusinessTypeService;
import com.artisan.medium.admin.service.UserService;
import com.artisan.model.BusinessType;
import com.artisan.model.User;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang on 2017/7/2.
 */
@Controller
@RequestMapping("/admin/btype/")
public class BusinessTypeController {
    private static final Logger log = LogManager.getLogger(BusinessTypeController.class);

    @Autowired
    public BusinessTypeService businessTypeService;

    @RequestMapping("business_type_list")
    public ModelAndView loginView(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Integer> result = new HashMap<>();
        return new ModelAndView("business_type/business_type_list", result);
    }
    @ResponseBody
    @RequestMapping(value = "business_type_select", method = RequestMethod.GET)
    public PageBean business_type_select(HttpServletRequest request, Integer limit, Integer pageNumber, int offset,int one_id,int two_id, String searchKey) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>(); // 业务条件查询参数
        paramMap.put("searchKey", searchKey);
        paramMap.put("one_id", one_id);
        paramMap.put("two_id", two_id);
        return businessTypeService.listPage(new PageParam(offset / limit + 1, limit), paramMap);
    }

    @ResponseBody
    @RequestMapping(value = "init_one_type", method = RequestMethod.GET)
    public ActionResult init_one_type(){
        ActionResult actionResult = new ActionResult();
        try {
            List<BusinessType> businessTypes = businessTypeService.getBusniessTypeByPid(0);
            actionResult.setCode(200);
            actionResult.setData(businessTypes);
        }catch (Exception e){
            actionResult.setCode(400);
            actionResult.setHint(e.getMessage());
        }
        return actionResult;
    }

    @ResponseBody
    @RequestMapping(value = "get_two_type", method = RequestMethod.GET)
    public ActionResult get_two_type(int pid){
        ActionResult actionResult = new ActionResult();
        try {
            List<BusinessType> businessTypes = businessTypeService.getBusniessTypeByPid(pid);
            actionResult.setCode(200);
            actionResult.setData(businessTypes);
        }catch (Exception e){
            actionResult.setCode(400);
            actionResult.setHint(e.getMessage());
        }
        return actionResult;
    }

    @ResponseBody
    @RequestMapping(value = "editType", method = RequestMethod.GET)
    public ActionResult editType(int id){
        ActionResult actionResult = new ActionResult();
        try {
            BusinessType businessTypes = businessTypeService.getBusniessTypeByiId(id);
            actionResult.setCode(200);
            actionResult.setData(businessTypes);
        }catch (Exception e){
            actionResult.setCode(400);
            actionResult.setHint(e.getMessage());
        }
        return actionResult;
    }

    @ResponseBody
    @RequestMapping(value = "updateType", method = RequestMethod.GET)
    public ActionResult editType(String type_name,int pid,int bid,String logo,String color){
        ActionResult actionResult = new ActionResult();
        try {
            BusinessType businessType = new BusinessType();
            businessType.setPid(pid);
            businessType.setbName(type_name);
            businessType.setbId(bid);
            businessType.setLogo(logo);
            businessType.setColor(color);
            businessTypeService.updateBusniessType(businessType);
            actionResult.setCode(200);
        }catch (Exception e){
            actionResult.setCode(400);
            actionResult.setHint(e.getMessage());
        }
        return actionResult;
    }

    @ResponseBody
    @RequestMapping(value = "saveOneType", method = RequestMethod.POST)
    public ActionResult saveOneType(String oneType){
        ActionResult actionResult=new ActionResult();
        BusinessType businessType=new BusinessType();
        businessType.setbName(oneType);
        businessType.setPid(0);
        if(businessTypeService.saveType(businessType)>0){
            actionResult.setCode(200);
        }else{
            actionResult.setCode(500);
        }
        return  actionResult;
    }

    @ResponseBody
    @RequestMapping(value = "saveTwoType", method = RequestMethod.POST)
    public ActionResult saveTwoType(String twoType,int pid,String logo,String color){
        ActionResult actionResult=new ActionResult();
        BusinessType businessType=new BusinessType();
        businessType.setbName(twoType);
        businessType.setPid(pid);
        businessType.setLogo(logo);
        businessType.setColor(color);
        if(businessTypeService.saveType(businessType)>0){
            actionResult.setCode(200);
        }else{
            actionResult.setCode(500);
        }
        return  actionResult;
    }

    @ResponseBody
    @RequestMapping(value = "delType" , method = RequestMethod.GET)
    public ActionResult delType(String ids){
        ActionResult actionResult=new ActionResult();
        if(businessTypeService.remvoeType(ids)>0){
            actionResult.setCode(200);
        }else{
            actionResult.setCode(500);
        }
        return  actionResult;
    }

    /**
     *
     * 查询翻新或者维修业务列表
     * @param flag
     * @return
     */
    @ResponseBody
    @RequestMapping("get_bus_type")
    public ActionResult getBusType(String flag) {
        int bid = businessTypeService.queryOneTypeId(flag);

        return get_two_type(bid);
    }

    /**
     * 查询所有二级业务类型列表
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("two_level_type")
    public ActionResult queryAllTwoLevelType(HttpServletRequest request, HttpServletResponse response) {

        ActionResult actionResult = new ActionResult();
        try {
            List<BusinessType> businessTypes = businessTypeService.queryAllTwoLevelType();
            actionResult.setCode(200);
            actionResult.setData(businessTypes);
        }catch (Exception e){
            actionResult.setCode(400);
            actionResult.setHint(e.getMessage());
        }
        return actionResult;
    }
}
